﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DemoGo
{
    public partial class FrmMain : Form
    {
        //游戏对象说明
        private Game _game;

        public Point point = new Point(0, 0);

        private new bool Click = false;

        public FrmMain()
        {
            InitializeComponent();
        }

        private void GameArea_Load(object sender, EventArgs e)
        {
            //初始化游戏类
            _game = new Game();
            //在FrmMain里调用_gameChanged事件
            _game._gameChanged += _game_gameChanged;
            _game._gameOver += _game__gameOver;
        }

        private void _game__gameOver()
        {
            if (_game.i==1)
            {
                //当游戏得分大于等于25分，显示你赢了
                if (_game.j>=25)
                {
                    MessageBox.Show("You Win!");
                }
                //否则显示游戏结束
                else
                {
                    MessageBox.Show("Game Over");
                }
                //重新开始游戏
                _game = new Game();
                //在FrmMain里调用_gameChanged事件
                _game._gameChanged += _game_gameChanged;
                _game._gameOver += _game__gameOver;
            }
        }

        private void _game_gameChanged()
        {
            if(_game!=null)
            {
                //重绘游戏区域
                gameArea.Invalidate();
            }
        }

        private void GameArea_Paint(object sender, PaintEventArgs e)
        {
            if(_game!=null)//游戏已经初始化了
            {
                if (Click==true)
                {
                    _game.Draw(e.Graphics, gameArea.Size);
                }
            }
        }

        //键盘输入
        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if(_game!=null)
            {
                //传递按键值给game
                _game.KeyDown(e.KeyCode.ToString());
                //重绘游戏区域
                gameArea.Invalidate();
            }
        }

        //关于我
        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutme frm = new FrmAboutme();
            frm.ShowDialog();
        }

        //游戏开始
        private void btnStart_Click(object sender, EventArgs e)
        {
            Click = true;
            //开始游戏
            _game = new Game();
            _game._gameChanged += _game_gameChanged;
            _game._gameOver += _game__gameOver;
            //重绘游戏区域
            gameArea.Invalidate();
        }

        //游戏操作帮助
        private void btnHelp_Click(object sender, EventArgs e)
        {
            FrmHelp frmHelp = new FrmHelp();
            frmHelp.ShowDialog();
        }
    }
}
